package com.example.com.transcribeeditpublish.fragement;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.com.transcribeeditpublish.R;
import com.example.com.transcribeeditpublish.TCVideoEditerListAdapter;
import com.example.com.transcribeeditpublish.utils.TCVideoEditerMgr;
import com.example.com.transcribeeditpublish.utils.TCVideoFileInfo;

import java.util.ArrayList;

/**
 * Created by i7-1 on 2018/12/11.
 */

public class PictureChooseFragment extends Fragment {

    private Context context;
    private boolean reload = false;
    private RecyclerView mRecyclerView;
    private TCVideoEditerListAdapter mAdapter;

    private Handler mMainHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            ArrayList<TCVideoFileInfo> fileInfoArrayList = (ArrayList<TCVideoFileInfo>) msg.obj;
            mAdapter.addAll(fileInfoArrayList);
        }
    };

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_picture, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        if (reload) {
            loadPictureList();
        }
    }

    private void initView() {
        mRecyclerView = (RecyclerView) getView().findViewById(R.id.rv);
        mRecyclerView.setLayoutManager(new GridLayoutManager(context, 4));
        mAdapter = new TCVideoEditerListAdapter(context);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setMultiplePick(true);
    }

    public void loadPictureList() {

        if (getActivity()==null) {
            reload = true;
            return;
        }
        ArrayList<TCVideoFileInfo> fileInfoArrayList = TCVideoEditerMgr.getAllPictrue(getActivity());
        Message msg = new Message();
        msg.obj = fileInfoArrayList;
        mMainHandler.sendMessage(msg);
    }

    public TCVideoEditerListAdapter getmAdapter() {
        return mAdapter;
    }
}
